package cn.jolyoulu.adminweb.controller.sys;



import cn.jolyoulu.adminweb.entity.bo.query.SysDictQueryBO;
import cn.jolyoulu.adminweb.entity.vo.SysDictDataVO;
import cn.jolyoulu.adminweb.entity.vo.SysDictTypeVO;
import cn.jolyoulu.common.mybatis.plus.plugin.page.JlPage;
import cn.jolyoulu.common.web.security.permission.Permission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import cn.jolyoulu.adminweb.entity.bo.form.SysDictDataFormBO;
import cn.jolyoulu.adminweb.entity.bo.form.SysDictFormBO;
import cn.jolyoulu.adminweb.service.ApiSysDictService;
import cn.jolyoulu.common.web.entity.ResultInfo;

import java.util.List;

/**
 * 系统管理/字典管理
 */
@RestController
@RequestMapping("/api/sys/dict")
public class ApiSysDictController {

    @Autowired
    private ApiSysDictService apiSysDictService;

    /**
     * 字典类型-列表查询
     */
    @Permission({"sys:dict:basic"})
    @GetMapping("/pageList")
    public ResultInfo<JlPage<SysDictTypeVO>> pageList(SysDictQueryBO bo){
        JlPage<SysDictTypeVO> res = apiSysDictService.pageList(bo);
        return ResultInfo.success(res);
    }

    /**
     * 字典类型-获取
     */
    @Permission({"sys:dict:basic"})
    @GetMapping("/get/{id}")
    public ResultInfo<SysDictTypeVO> getById(@PathVariable String id) {
        SysDictTypeVO res = apiSysDictService.getById(id);
        return ResultInfo.success(res);
    }

    /**
     * 字典类型-添加
     */
    @Permission({"sys:dict:add"})
    @PostMapping("/add")
    public ResultInfo add(@RequestBody @Validated SysDictFormBO sysDictFormBO){
        apiSysDictService.add(sysDictFormBO);
        return ResultInfo.success();
    }

    /**
     * 字典类型-编辑
     */
    @Permission({"sys:dict:edit"})
    @PutMapping("/edit/{id}")
    public ResultInfo edit(@PathVariable String id,@RequestBody SysDictFormBO sysDictFormBO){
        sysDictFormBO.setId(id);
        apiSysDictService.update(sysDictFormBO);
        return ResultInfo.success();
    }

    /**
     * 字典类型-删除
     */
    @Permission({"sys:dict:del"})
    @DeleteMapping("/del")
    public ResultInfo del(@RequestBody List<String> ids){
        apiSysDictService.delByIds(ids);
        return ResultInfo.success();
    }

    /**
     * 字典数据-列表查询
     */
    @Permission({"sys:dict:basic"})
    @GetMapping("/data/list/{dictTypeId}")
    public ResultInfo dataList(@PathVariable String dictTypeId){
        List<SysDictDataVO> res = apiSysDictService.dataList(dictTypeId);
        return ResultInfo.success(res);
    }

    /**
     * 字典数据-获取
     */
    @Permission({"sys:dict:add"})
    @GetMapping("/data/get/{id}")
    public ResultInfo getDataById(@PathVariable String id){
        SysDictDataVO res = apiSysDictService.getDataById(id);
        return ResultInfo.success(res);
    }

    /**
     * 字典数据-添加
     */
    @Permission({"sys:dict:add"})
    @PostMapping("/add/data")
    public ResultInfo addData(@RequestBody @Validated SysDictDataFormBO sysDictDataFormBO){
        apiSysDictService.addData(sysDictDataFormBO);
        return ResultInfo.success();
    }

    /**
     * 字典数据-编辑
     */
    @Permission({"sys:dict:edit"})
    @PutMapping("/edit/data/{id}")
    public ResultInfo editData(@PathVariable String id,@RequestBody SysDictDataFormBO sysDictDataFormBO){
        sysDictDataFormBO.setId(id);
        apiSysDictService.editData(sysDictDataFormBO);
        return ResultInfo.success();
    }

    /**
     * 字典数据-删除
     */
    @Permission({"sys:dict:del"})
    @DeleteMapping("/del/data")
    public ResultInfo delData(@RequestBody List<String> ids){
        apiSysDictService.delData(ids);
        return ResultInfo.success();
    }

}
